package org.example.warehouse.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.example.product.domain.Product;
import org.example.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.warehouse.domain.MallStoreDetail;
import org.example.warehouse.service.IMallStoreDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仓库存储详情Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-12
 */
@Controller
@RequestMapping("/warehouse/mallStoreDetail")
public class MallStoreDetailController extends BaseController
{
    private String prefix = "warehouse/mallStoreDetail";

    @Autowired
    private IMallStoreDetailService mallStoreDetailService;

    @Autowired
    private IProductService productService;

    @GetMapping()
    public String mallStoreDetail(MallStoreDetail mallStoreDetail,ModelMap mmp)
    {
        mmp.put("mallStoreDetail",mallStoreDetail);
        return prefix + "/mallStoreDetail";
    }

    /**
     * 查询仓库存储详情列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MallStoreDetail mallStoreDetail)
    {
        startPage();
        List<MallStoreDetail> list = mallStoreDetailService.selectMallStoreDetailList(mallStoreDetail);
        return getDataTable(list);
    }

    /**
     * 导出仓库存储详情列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MallStoreDetail mallStoreDetail)
    {
        List<MallStoreDetail> list = mallStoreDetailService.selectMallStoreDetailList(mallStoreDetail);
        ExcelUtil<MallStoreDetail> util = new ExcelUtil<MallStoreDetail>(MallStoreDetail.class);
        return util.exportExcel(list, "仓库存储详情数据");
    }

    /**
     * 新增仓库存储详情
     */
    @GetMapping("/add")
    public String add(Product product , ModelMap mmp)
    {
        startPage();
        mmp.put("productList", productService.selectMallStoreProductList(product));
        return prefix + "/add";
    }

    private Long getStoreDetailCode() {
        MallStoreDetail storeDetail =new MallStoreDetail();
        storeDetail.setCreateTime(DateUtils.getNowDate());
        startPage();
        List<MallStoreDetail> list = mallStoreDetailService.selectMallStoreDetailList(storeDetail);
        TableDataInfo dataTable = getDataTable(list);
        Long total =dataTable.getTotal()+1L;
//        String s2 = String.format("%04d", total);
//        String contractCode =  DateUtils.dateTimeNow("yyMMdd")+s2;
        return total;
    }

    /**
     * 新增保存仓库存储详情
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MallStoreDetail mallStoreDetail)
    {
         mallStoreDetail.getParams().put("code",getStoreDetailCode());
        return toAjax(mallStoreDetailService.insertMallStoreDetail(mallStoreDetail));
    }

    /**
     * 修改仓库存储详情
     */
    @GetMapping("/edit/{storeDetailId}")
    public String edit(@PathVariable("storeDetailId") Long storeDetailId, ModelMap mmap)
    {
        MallStoreDetail mallStoreDetail = mallStoreDetailService.selectMallStoreDetailByStoreDetailId(storeDetailId);
        mmap.put("mallStoreDetail", mallStoreDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存仓库存储详情
     */
    @Log(title = "仓库存储详情", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MallStoreDetail mallStoreDetail)
    {
        return toAjax(mallStoreDetailService.updateMallStoreDetail(mallStoreDetail));
    }

    /**
     * 删除仓库存储详情
     */
    @Log(title = "仓库存储详情", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(mallStoreDetailService.deleteMallStoreDetailByStoreDetailIds(ids));
    }
}
